import sys,math,os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class TableDemo(QWidget):
    def __init__(self,arg=None):
        super(TableDemo,self).__init__(arg)
        self.setWindowTitle("tableview案例")
        self.resize(600,300)
        self.btn=QPushButton("删除当前行")
        self.btn.clicked.connect(self.deleterow)
        self.model=QStandardItemModel(4,4)
        self.model.setHorizontalHeaderLabels(["标题1","标题2","标题3","标题4"])
        for row in  range(4):
            for column in range(4):
                item=QStandardItem("row %s,column %s"%(row,column))
                self.model.setItem(row,column,item)
        self.tableView=QTableView()
        self.tableView.setModel(self.model)
        #设置100xianshi
        self.tableView.horizontalHeader().setStretchLastSection(True)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        #手动添加
        self.model.appendRow([
            QStandardItem("row %s,column %s" % (22, 22)),
            QStandardItem("row %s,column %s" % (33, 33)),
        ]
        )
        #想删除某一行

        dlgLayout=QVBoxLayout()
        dlgLayout.addWidget(self.tableView)
        dlgLayout.addWidget(self.btn)
        self.setLayout(dlgLayout)

    def deleterow(self):
        indexs=self.tableView.selectionModel().selection().indexes()
        if len(indexs)>0:
            index=indexs[0]
            self.model.removeRows(index.row(),1)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    table=TableDemo()
    table.show()
    sys.exit(app.exec_())